package com.ssm.service.Impl;

import com.ssm.dao.CustomerDao;
import com.ssm.pojo.Customer;
import com.ssm.pojo.Enter;
import com.ssm.pojo.Fee;
import com.ssm.pojo.House;
import com.ssm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//客户服务实现类
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    //查询所有客户信息
    @Override
    public List<Customer> getAllCustomers() {
        return customerDao.queryAllCustomers();
    }

    //根据姓名查找客户
    @Override
    public Customer getCustomerByName(String name) {
        return customerDao.queryCustomerByName(name);
    }

    @Override
    public Customer queryCustomerByCard(String card) {
        return customerDao.queryCustomerByCard(card);
    }

    //查询所有房间信息
    @Override
    public List<House> getAllHouses() {
        return customerDao.queryAllHouses();
    }


    //添加入住者信息
    @Override
    public int addCustomer(Customer customer) {
        return customerDao.addCustomer(customer);
    }

    //得到要入住的房间信息
    @Override
    public House getHouse(String name) {
        return customerDao.queryOneHouse(name);
    }

    //插入订单部分信息
    @Override
    public Map<String, Object> addFee(Fee fee) {
        Map<String, Object> map = new HashMap<>();
        int i = customerDao.addFee(fee);
        map.put("i", i);
        map.put("fee", fee);
        return map;
    }

    //Enter
    //预订房间
    @Override
    public String bookRoom(Customer customer, String houseName, Enter enter, Fee fee) {
        String msg = "添加失败";
        int i1 = customerDao.addCustomer(customer);
        if (i1 > 0) System.out.println("用户信息添加成功");
        House house = customerDao.queryOneHouse(houseName);
        if (house != null) {
            if ("空闲".equals(house.getState())) {
                //如果该房间空闲
                //整理生成账单信息     在此之前会封装部分账单的信息 这里完善一部分信息
                fee.setCustomer(customer.getId());
                fee.setHouseId(house.getId());
                fee.setUserId(1);
                int i2 = customerDao.addFee(fee);
                //完善部分入住信息 （主要是来自其他表的主键）
                enter.setCustomerId(customer.getId());
                enter.setHouseId(house.getId());
                enter.setFeeInfo("" + fee.getId());
                enter.setUserId(1);
                int i3 = customerDao.toEnter(enter);
                house.setState("在住");
                house.setCustomerId(customer.getId());
                int i4 = customerDao.reduceHouse(house);
                //最后将enterId添加进fee表
                int i5 = customerDao.addEnterId(enter.getId(), fee.getId());
                if (i1 > 0 && i2 > 0 && i3 > 0 && i4 > 0 && i5 > 0) {
                    msg = "添加成功";
                    return msg;
                } else {
                    throw new RuntimeException();
                }
            }
            return msg;
        }
        return msg;
    }


    //退订房间
    @Override
    public int checkOut(int houseId) {
        return customerDao.checkOut(houseId);
    }

    @Override
    public int update(Customer customer) {
        return 0;
    }

    @Override
    public int delete(String card) {
        return 0;
    }
}
